package cn.jingyuan.swan.cloud.core;

import cn.jingyuan.bee.exception.ErrorCode;

public class RestResultUtils {

    private static final int SC_OK = 200;

    private static final int SC_INTERNAL_SERVER_ERROR = 500;

    public static <D> RestResult<D> ok() {
        return new <D>RestResult<D>()
            .setCode(ExCode.SUCCESS.getCode())
            .setCodeMsg(ExCode.SUCCESS.getCodeMsg())
            .setHttpStatus(SC_OK);
    }

    public static <D> RestResult<D> ok(D data) {
        return new <D>RestResult<D>()
            .setCode(ExCode.SUCCESS.getCode())
            .setCodeMsg(ExCode.SUCCESS.getCodeMsg())
            .setData(data);
    }

    public static <D> RestResult<D> failure() {
        return new <D>RestResult<D>()
            .setCode(ExCode.FAILURE.getCode())
            .setCodeMsg(ExCode.FAILURE.getCodeMsg());
    }

    public static <D> RestResult<D> warning() {
        return new <D>RestResult<D>()
            .setCode(ExCode.WARNING.getCode())
            .setCodeMsg(ExCode.WARNING.getCodeMsg())
            .setHttpStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> warning(String warning) {
        return new <D>RestResult<D>()
            .setCode(ExCode.WARNING.getCode())
            .setCodeMsg(warning)
            .setHttpStatus(SC_INTERNAL_SERVER_ERROR);
    }

    public static <D> RestResult<D> instance(ErrorCode exCode) {
        return new <D>RestResult<D>()
            .setCode(exCode.getCode())
            .setCodeMsg(exCode.getCodeMsg());
    }

}
